var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/Menus/index.tsx
var Menus_exports = {};
__export(Menus_exports, {
  default: () => Menus
});
module.exports = __toCommonJS(Menus_exports);
var import_AppstoreOutlined = __toESM(require("@ant-design/icons/AppstoreOutlined"));
var import_hooks = require("@szzj/hooks");
var import_antd = require("antd");
var import_style = require("antd/es/menu/style/index.js");
var import_react = __toESM(require("react"));
var import_react_router_dom = require("react-router-dom");
var import_index = require("./index.less");
function Menus({
  pathname = "/",
  base,
  routes,
  mode = "inline",
  sensitive = true,
  onClick,
  renderMenuTitle,
  IconComponents,
  ...rest
}) {
  const [openKeys, setOpenKeys] = (0, import_react.useState)([]);
  const [selectedKeys, setSelectedKeys] = (0, import_react.useState)([]);
  const tree = (0, import_hooks.useTree)(routes, {
    uniqueKey: "key",
    childrenKey: "routes"
  });
  const treeRef = (0, import_hooks.useRealtimeRef)(tree);
  const baseChildRoutes = (0, import_react.useMemo)(() => {
    if (base && base !== "/") {
      const baseRoute = treeRef.current.findNode(base);
      if (!baseRoute)
        return routes;
      return (baseRoute == null ? void 0 : baseRoute.title) ? [baseRoute] : (baseRoute == null ? void 0 : baseRoute.routes) ?? [];
    }
    return routes;
  }, [base, routes]);
  (0, import_react.useEffect)(() => {
    const defaultOpenedRoutes = treeRef.current.filter(
      baseChildRoutes,
      (route) => !!route.menu && !!route.defaultOpened
    );
    const defaultOpenedRoutesArr = treeRef.current.toArray(defaultOpenedRoutes);
    setOpenKeys(defaultOpenedRoutesArr.map((route) => route.key));
  }, [baseChildRoutes]);
  const openKeysRef = (0, import_hooks.useRealtimeRef)(openKeys);
  const sensitiveRef = (0, import_hooks.useRealtimeRef)(sensitive);
  (0, import_react.useEffect)(() => {
    const baseChildRoutesArr = treeRef.current.toArray(baseChildRoutes);
    const matchedRoutes = treeRef.current.filter(
      baseChildRoutesArr,
      (route) => !!route.menu && !!(0, import_react_router_dom.matchPath)({ path: route.path }, pathname)
    );
    treeRef.current.forEach(matchedRoutes, (route) => {
      var _a;
      if ((_a = route.routes) == null ? void 0 : _a.length) {
        if (!openKeysRef.current.includes(route.key)) {
          if (sensitiveRef.current) {
            setOpenKeys((keys) => [...keys, route.key]);
          }
        }
      } else if (route.path.split("/").length === pathname.split("/").length) {
        if (!selectedKeys.includes(route.key)) {
          setSelectedKeys([route.key]);
        }
      }
    });
  }, [baseChildRoutes, pathname]);
  const renderIcon = (0, import_react.useCallback)(
    (route, level) => {
      const defaultCustomIcon = IconComponents && IconComponents.default;
      const DefaultIcon = !level ? defaultCustomIcon || import_AppstoreOutlined.default : void 0;
      let Icon = route.Icon || DefaultIcon;
      if (!route.Icon && route.icon) {
        if (IconComponents) {
          Icon = IconComponents[route.icon];
          route.Icon = Icon;
        } else {
          Icon = import_react.default.lazy(() => {
            let result;
            try {
              result = require(`@ant-design/icons/${route.icon}.js`);
            } catch (err) {
              console.warn(
                `icon: ${route.icon} is not available. @szzj/layout will use default icon.`
              );
              result = { default: DefaultIcon };
            }
            return Promise.resolve(result);
          });
          route.Icon = Icon;
          route.lazy = true;
          return /* @__PURE__ */ import_react.default.createElement("span", { className: "anticon szzj-menu-icon" }, /* @__PURE__ */ import_react.default.createElement(import_react.default.Suspense, { fallback: null }, /* @__PURE__ */ import_react.default.createElement(Icon, null)));
        }
      }
      if (route.lazy && Icon) {
        return /* @__PURE__ */ import_react.default.createElement("span", { className: "anticon szzj-menu-icon" }, /* @__PURE__ */ import_react.default.createElement(import_react.default.Suspense, { fallback: null }, /* @__PURE__ */ import_react.default.createElement(Icon, null)));
      }
      return Icon ? /* @__PURE__ */ import_react.default.createElement("span", { className: "anticon szzj-menu-icon" }, Icon ? /* @__PURE__ */ import_react.default.createElement(Icon, null) : null) : null;
    },
    [IconComponents]
  );
  const getItem = (0, import_react.useCallback)(
    (route, level = 0) => {
      var _a;
      const icon = renderIcon(route, level);
      const title = renderMenuTitle ? renderMenuTitle(route, level) : route.title;
      if ((_a = route.routes) == null ? void 0 : _a.length) {
        return {
          key: route.key,
          icon,
          label: title,
          title: route.title,
          disabled: route.disabled,
          danger: route.danger,
          children: route.routes.map((childRoute) => {
            return getItem(childRoute, level + 1);
          })
        };
      } else {
        return {
          key: route.key,
          icon,
          label: route.target || onClick ? title : /* @__PURE__ */ import_react.default.createElement(import_react_router_dom.Link, { to: route.path }, title),
          title: route.title,
          disabled: route.disabled,
          danger: route.danger
        };
      }
    },
    [renderIcon, onClick, renderMenuTitle]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Menu,
    {
      mode,
      ...rest,
      selectedKeys,
      openKeys,
      onOpenChange: setOpenKeys,
      items: baseChildRoutes.filter((route) => !!route.menu).map((route) => getItem(route, 0)),
      onClick: ({ key }) => {
        const route = treeRef.current.findNode(key);
        if (onClick)
          onClick(route);
        else if (route.target)
          window.location.href = route.target;
      }
    }
  );
}
